# scripts/levels/Level_Desert.gd
class_name LevelDesert
extends Node2D # Or Node, depending on what the root of your Level_Desert.tscn is

# References to nodes within Level_Desert.tscn (if needed)
@onready var sandstorm_timer: Timer = $SandstormTimer
@onready var sandstorm_effect: CanvasModulate = $SandstormEffect # Or ColorRect

# Sandstorm parameters
var sandstorm_active: bool = false
@export var sandstorm_duration: float = 10.0 # How long the sandstorm lasts
@export var sandstorm_interval_min: float = 20.0 # Minimum time between sandstorms
@export var sandstorm_interval_max: float = 40.0 # Maximum time between sandstorms
@export var sandstorm_intensity: Color = Color(0.8, 0.7, 0.5, 0.3) # Tint color and alpha

func _ready():
	print("Level Desert Ready")
	if not sandstorm_timer:
		printerr("SandstormTimer node not found in Level_Desert scene!")
		return
	if not sandstorm_effect:
		printerr("SandstormEffect node not found in Level_Desert scene!")
		# Fallback or alternative effect?
	
	sandstorm_timer.timeout.connect(_on_sandstorm_timer_timeout)
	# Start with a random interval
	sandstorm_timer.wait_time = randf_range(sandstorm_interval_min, sandstorm_interval_max)
	sandstorm_timer.start()

	# Initialize effect as inactive
	if sandstorm_effect:
		sandstorm_effect.color = Color(1, 1, 1, 0) # Fully transparent white initially

func _on_sandstorm_timer_timeout():
	if sandstorm_active:
		# Sandstorm ends
		sandstorm_active = false
		print("Sandstorm ended.")
		tween_sandstorm_effect(Color(1, 1, 1, 0), 1.0) # Fade out
		# TODO: Restore player vision or remove other effects if implemented

		# Schedule next sandstorm
		sandstorm_timer.wait_time = randf_range(sandstorm_interval_min, sandstorm_interval_max)
		sandstorm_timer.start()
	else:
		# Sandstorm starts
		sandstorm_active = true
		print("Sandstorm started!")
		tween_sandstorm_effect(sandstorm_intensity, 1.0) # Fade in
		# TODO: Reduce player vision range or apply other effects

		# Set timer for sandstorm duration
		sandstorm_timer.wait_time = sandstorm_duration
		sandstorm_timer.start()

func tween_sandstorm_effect(target_color: Color, duration: float):
	if not sandstorm_effect: return

	var tween = get_tree().create_tween()
	tween.tween_property(sandstorm_effect, "color", target_color, duration).set_trans(Tween.TRANS_LINEAR).set_ease(Tween.EASE_IN_OUT)


# Optional: Provide enemy list for this level if using dynamic loading in MainGame
# func get_enemy_list() -> Array[PackedScene]:
# 	return [
# 		preload("res://scenes/enemies/common/DesertLizard.tscn"), # Example
# 		preload("res://scenes/enemies/common/SandWorm.tscn"),   # Example
# 		preload("res://scenes/enemies/common/Skeleton.tscn")
# 	]

# Optional: Define Boss for this level
# func get_boss_scene() -> PackedScene:
#   return preload("res://scenes/enemies/boss/DesertLord.tscn")
